package com.cn.goodsadmin.dao.impl;


import com.cn.goodsadmin.dao.BaseDao;
import com.cn.goodsadmin.dao.IActionRoutDao;
import com.cn.goodsadmin.po.ActionRout;
import com.cn.goodsadmin.util.TimeUtil;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository("actionroutDao")
public class ActionRoutDaoImpl extends BaseDao<ActionRout> implements IActionRoutDao {

    public ActionRout getActionByDate(final Date date, final String openid, final int appid) throws Exception {
        final StringBuilder builder = new StringBuilder();
        builder.append("from ActionRout ar where date(ar.cTime)=?0");
        builder.append(" and ar.cOpenid=?1");
        builder.append(" and ar.cAppid=?2");
        return getHibernateTemplate().execute(new HibernateCallback<ActionRout>() {
            public ActionRout doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(builder.toString());
                query.setParameter(0, date);
                query.setParameter(1, openid);
                query.setParameter(2,appid);
                //TODO 测试阶段先关闭
                //query.setCacheable(true);
                query.setLockMode("ar", LockMode.PESSIMISTIC_WRITE);
                List<ActionRout> actionRouts = query.list();
                if (actionRouts != null && actionRouts.size() > 0) {
                    return actionRouts.get(0);
                }
                return null;
            }
        });
    }

    public List<ActionRout> getActionRoutByDay(int day,int appid) throws Exception {
        StringBuilder builder = new StringBuilder();
        List<Object> paraList = new ArrayList<Object>();
        builder.append("from ActionRout a where date(a.cTime)<?0");
        paraList.add(TimeUtil.getCurrDay(0));
        builder.append("  and date(a.cTime)>?1");
        paraList.add(TimeUtil.getCurrDay(-1 * day));
        builder.append(" and a.cAppid=?2");
        paraList.add(appid);
        return find(builder.toString(), paraList.toArray());
    }


    public long getUserActionRountByBeginTimeAndEndTime(Date beginTime, Date endTime, String openid,int appid) throws Exception {
        //SELECT d_mama_dish.t_order_info.c_creator , count( * ) AS count
        //FROM  d_mama_dish.t_order_info
        //GROUP BY c_openid
        //ORDER BY count DESC
        //LIMIT 100
        StringBuilder builder=new StringBuilder();
        builder.append("select count( * )  from ActionRout ar ");
        builder.append(" where date(ar.cTime)>=?0");
        builder.append("  and date(ar.cTime)<=?1");
        builder.append("  and ar.cOpenid=?2");
        builder.append("  and ar.cAppid=?3");
        List<Object> paraList=new ArrayList<Object>();
        paraList.add(beginTime);
        paraList.add(endTime);
        paraList.add(openid);
        paraList.add(appid);
        Long count = (Long)find(builder.toString(),paraList.toArray()).listIterator().next();
        return count.longValue();
    }


    public long getActionRountByDay(Date daytime,int appid) throws Exception {
        StringBuilder builder=new StringBuilder();
        builder.append("select count(*) from ActionRout a where date(a.cTime)=?0");
        builder.append(" and a.cAppid=?1");
        List<Object> paraList = new ArrayList<Object>();
        paraList.add(daytime);
        paraList.add(appid);
        Long count = (Long)find(builder.toString(),paraList.toArray()).listIterator().next();
        return count.longValue();
    }
}
